package com.qujia.station.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.utils.PageInfoUtil;
import com.qujia.common.utils.StringUtils;
import com.qujia.station.domain.OrderPayType;
import com.qujia.station.domain.vo.OrderPayTypeResVo;
import com.qujia.station.service.OrderPayTypeService;
import com.qujia.system.domain.vo.ResponseVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/order/payType")
@Slf4j
public class OrderPayTypeController {

    @Autowired
    private OrderPayTypeService orderPayTypeService;

    @PostMapping("/name")
    @ApiOperation(value = "获取名称", httpMethod = "POST",response = OrderPayType.class)
    public AjaxResult getPayTypeName(@RequestBody OrderPayType orderPayType){
        log.info("getPayTypeName获取名称请求参数：[{}]", JSONObject.toJSONString(orderPayType));
        List<OrderPayType> orderPayTypeList = orderPayTypeService.selectOrderPayTypeName(orderPayType);
        return AjaxResult.success(orderPayTypeList);
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取支付方式列表", httpMethod = "POST")
    public AjaxResult getPayTypeList(@RequestBody OrderPayType orderPayType){
        if(Objects.isNull(orderPayType) || Objects.isNull(orderPayType.getPageNum())
                || Objects.isNull(orderPayType.getPageSize())){
            return AjaxResult.error("请求参数异常");
        }
        log.info("getPayTypeList获取支付方式列表请求参数：[{}]",JSONObject.toJSONString(orderPayType));
        List<OrderPayType> orderPayTypeList = orderPayTypeService.selectOrderPayTypeList(orderPayType);
        log.info("getPayTypeList获取支付方式列表 返回结果：[{}],数量：[{}]",JSONObject.toJSONString(orderPayTypeList),orderPayTypeList.size());
        // 手动分页
        PageInfo pageInfo = PageInfoUtil.listPageInfo(orderPayType.getPageNum(), orderPayType.getPageSize(), orderPayTypeList);
        return AjaxResult.success(pageInfo);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存支付方式", httpMethod = "POST")
    public AjaxResult saveOrderPayType(@RequestBody OrderPayType orderPayType){
        if(Objects.isNull(orderPayType) || StringUtils.isEmpty(orderPayType.getPayTypeName())
                || Objects.isNull(orderPayType.getStationId())){
            return AjaxResult.error("请求参数异常");
        }
        int i = orderPayTypeService.saveOrderPayType(orderPayType);
        if(i <= 0){
            return AjaxResult.error("保存支付方式失败");
        }
        return AjaxResult.success("保存成功");
    }

    @PostMapping("/update")
    @ApiOperation(value = "编辑支付方式", httpMethod = "POST")
    public AjaxResult updateOrderPayType(@RequestBody OrderPayType orderPayType){
        if(Objects.isNull(orderPayType) || StringUtils.isEmpty(orderPayType.getPayTypeName())
                || Objects.isNull(orderPayType.getId())){
            return AjaxResult.error("请求参数异常");
        }
        int i = orderPayTypeService.updateOrderPayType(orderPayType);
        if(i <= 0){
            return AjaxResult.error("编辑支付方式失败");
        }
        return AjaxResult.success("编辑成功");
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除支付方式", httpMethod = "GET")
    public AjaxResult deleteOrderPayType(@RequestParam Long id){
        log.info("deleteOrderPayType删除支付方式配置：[{}]",id);
        int i = orderPayTypeService.deleteOrderPayType(id);
        if(i <= 0){
            return AjaxResult.error("删除支付方式失败");
        }
        return AjaxResult.success("删除成功");
    }

    @GetMapping("/query")
    @ApiOperation(value = "获取支付方式列表", httpMethod = "GET")
    public AjaxResult queryPayTypeList(String payTypeName) {
        List<OrderPayTypeResVo> orderPayTypeList = orderPayTypeService.queryOrderPayTypeList(payTypeName);
        return AjaxResult.success(new ResponseVo<>(orderPayTypeList, new PageInfo(orderPayTypeList).getTotal()));
    }
}
